import time
from appium.webdriver import Remote
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from app.swipe import BaesPages

desired_caps = {
    'platformName': 'Android',
    'deviceName': 'emulator-5554',
    'appPackage': 'com.baidu.searchbox',
    'appActivity': 'com.baidu.searchbox.SplashActivity',
    'platformVersion': '7.1.2',
    'automationName': 'UiAutomator2',
    'noReset': False,
    'unicodeKeyboard': True,
    'resetKeyboard': True,
    'autoGrantPermissions': True
}

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(10)
# 锁屏
driver.lock()
driver.back()
time.sleep(2)
# 解开屏幕
driver.unlock()
time.sleep(2)

# 向上滑动
base_page = BaesPages(driver)
base_page.swipe(direct='up')

# 定位锁屏的元素
el = driver.find_element()
# 获取元素的属性：x,y,width,heigth
rect = el.rect
start_x = rect['x']
start_y = rect['y']
width = rect['width']
height = rect['height']

point_01 = {'x': start_x + width * 1 / 6, 'y': start_y + height * 1 / 6}
point_02 = {'x': start_x + width * 1 / 2, 'y': start_y + height * 1 / 6}
point_03 = {'x': start_x + width * 5 / 6, 'y': start_y + height * 1 / 6}
point_04 = {'x': start_x + width * 1 / 6, 'y': start_y + height * 1 / 2}
point_05 = {'x': start_x + width * 1 / 2, 'y': start_y + height * 1 / 2}
point_06 = {'x': start_x + width * 5 / 6, 'y': start_y + height * 1 / 2}
point_07 = {'x': start_x + width * 1 / 6, 'y': start_y + height * 5 / 6}
point_08 = {'x': start_x + width * 1 / 2, 'y': start_y + height * 5 / 6}
point_09 = {'x': start_x + width * 5 / 6, 'y': start_y + height * 5 / 6}

# 调用touchAction,执行解锁操作
action = TouchAction(driver)
action.press(**point_01).wait(200).\
    move_to(**point_02).wait(200).\
    move_to(**point_03).wait(200).\
    move_to(**point_05).wait(200).\
    move_to(**point_07).wait(200).\
    move_to(**point_08).wait(200).\
    move_to(**point_09).wait(200).\
    release().perform()

